package com.cloudlinkin.util.http;

import com.cloudlinkin.util.encrypt.SHA1;

import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;


public class ParamUtilForHFX {

	/**
	 * 整理成hfx适应的参数格式
	 * 
	 * @param map
	 * @return
	 */
	public static String toHfxParam(Map<String, Object> map,String appKey) {

		String param = "";
		try {
			param = SHA1.encode(accessParam(map,appKey));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return param;

	}

	/**
	 * 排序
	 * 
	 * @param map
	 * @return
	 */
	public static String accessParam(Map<String, Object> map,String appKey) {
		StringBuffer sb = new StringBuffer();
		try {

			List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(
					map.entrySet());

			// 对HashMap中的key 进行排序
			Collections.sort(infoIds,
					new Comparator<Map.Entry<String, Object>>() {
						public int compare(Map.Entry<String, Object> o1,
								Map.Entry<String, Object> o2) {
							return (o1.getKey()).toString().compareTo(
									o2.getKey().toString());
						}
					});
			//  
			for (int i = 0; i < infoIds.size(); i++) {
				String id = infoIds.get(i).toString();
				sb.append(id);
			}
			sb.append(appKey);
		} catch (Exception e) {

		}
		System.out.println("排序后的结果"+sb.toString());
		return sb.toString();
	}

 

	/**
	 * aes加密
	 * 
	 * @param param
	 * @return
	 */
	public static String Aesparam1(String param) {

		String resultParam = "";
		try {
			
			//resultParam = Aes.aesEncrypt(param, ConstantForApp.HFX_YIQISHANGDAXUE_APPKEY_TEST.substring(16));
//			AdcCrypto crypt = new AdcCrypto(
//					ConstantForApp.HFX_YIQISHANGDAXUE_APPKEY_TEST.substring(16));
//			resultParam = crypt.enCode(param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultParam;
	}
	
 

	
	 /** 
     * SHA1 安全加密算法 
     * @param maps 参数key-value map集合 
     * @return 
     * @throws DigestException  
     */  
    public static String SHA1(String decrypt) throws DigestException {  
       
        try {  
            //指定sha1算法  
            MessageDigest digest = MessageDigest.getInstance("SHA-1");  
            digest.update(decrypt.getBytes());  
            //获取字节数组  
            byte messageDigest[] = digest.digest();  
            // Create Hex String  
            StringBuffer hexString = new StringBuffer();  
            // 字节数组转换为 十六进制 数  
            for (int i = 0; i < messageDigest.length; i++) {  
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);  
                if (shaHex.length() < 2) {  
                    hexString.append(0);  
                }  
                hexString.append(shaHex);  
            }  
            return hexString.toString().toUpperCase();  
  
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
            throw new DigestException("签名错误！");  
        }  
    }  
    
    //app_key=98366c1c8d8949deb19a56bda64a7e2fchannel=98366c1c8d8949deb19a56bda64a7e2fcontactId=48757987enterpriseId=7073008once=a9I258E7v6Ahsdk_from=javaversion=2.0f93dba8a123f4b5ba6525f74f40074de
}
